/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grass.plugin.dialect;

import com.grass.plugin.paging.page.Page;

/**
 * @author wujx
 * QQ 396246973
 * Date 2013-02-10
 */
public class OracleDialect extends Dialect {

	@Override
    public String getLimitString(String sql, Page page) {
        if (page.isPagination() == false) {
            return sql;
        }
        boolean isForUpdate = false;
        if (sql.toLowerCase().endsWith(" for update")) {
            sql = sql.substring(0, sql.length() - 11);
            isForUpdate = true;
        }
        StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
        pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
        pagingSelect.append(sql);
        //pagingSelect.append(" ) row_ ) where rownum_ <= ").append(page.getPageEndRow()).append(" and rownum_ > ").append(page.getPageStartRow()).append("");
        pagingSelect.append(" ) row_ ) where rownum_ ");
        pagingSelect.append("between ").append(page.getPageStartRow()).append(" + 1").append(" and ").append(page.getPageEndRow());
        if (isForUpdate) {
            pagingSelect.append(" for update");
        }
        return pagingSelect.toString();
    }
    
	@Override
	public String Escape(String sql,String lmark, String rmark) {	   
		return sql.replaceAll(lmark == null? "\\[" : lmark, "\\\"").replaceAll(rmark == null? "\\]" : rmark, "\\\"");
	}
}
